package com.xxj.fflink.menu;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.view.MotionEvent;

import com.xxj.fflink.Main;
import com.xxj.fflink.R;

public class GameMenuView extends MyGameView {

	private Main mainApp;

	private Bitmap imgStart;
	// private Bitmap imgOpt;
	// private Bitmap imgRes;
	private Bitmap imgHelp;
	private Bitmap imgQuit;
	private Bitmap imgMoreGame;
	private Bitmap imgMenuBack;
	private Bitmap imgGameHelpText;

	int offX;
	int offY = 180;
	int itemMargin;
	// int optY;
	// int resY;
	int helpY;
	int quitY;
	int moreGameY;
	int gameHelpX, gameHelpY;

	int stateNormal = 1;
	int stateHelp = 2;
	int drawState = stateNormal;

	public GameMenuView(Context context) {
		super(context);

		mainApp = (Main) context;

		paint.setColor(Color.RED);
		paint.setTextSize(32);

		imgMenuBack = Util.getBitMap(this.getContext(),
				R.drawable.game_backgroud);
		imgStart = Util.getBitMap(this.getContext(), R.drawable.btn_start);
		// imgOpt = Util.getBitMap(this.getContext(), R.drawable.option);
		// imgRes = Util.getBitMap(this.getContext(), R.drawable.top_score);
		imgHelp = Util.getBitMap(this.getContext(), R.drawable.btn_help);
		imgMoreGame = Util.getBitMap(this.getContext(),
				R.drawable.btn_more_game);
		imgQuit = Util.getBitMap(this.getContext(), R.drawable.btn_exit);
		imgGameHelpText = Util.getBitMap(this.getContext(),
				R.drawable.game_help_text);

		offX = (W - imgStart.getWidth()) / 2;

		itemMargin = imgStart.getHeight() + 10;
		// optY = offY + itemMargin * 1;
		// resY = offY + itemMargin * 2;
		helpY = offY + itemMargin * 1;
		moreGameY = offY + itemMargin * 2;
		quitY = H - imgQuit.getHeight();

		gameHelpX = (W - imgGameHelpText.getWidth()) / 2;
		gameHelpY = (H - imgGameHelpText.getHeight()) / 2;

		menuItemActions
				.add(new MenuItemAction(GameAction.START,
						new Rect(offX, offY, imgStart.getWidth() + offX,
								imgStart.getHeight() + offY)));
		// menuItemActions.add(new MenuItemAction(GameAction.OPTION, new
		// Dimension(offX,
		// optY, imgOpt.getWidth(), imgOpt.getHeight())));
		// menuItemActions.add(new MenuItemAction(GameAction.TOP_SCORE, new
		// Dimension(offX,
		// resY, imgRes.getWidth(), imgRes.getHeight())));
		menuItemActions
				.add(new MenuItemAction(GameAction.HELP, new Rect(offX, helpY,
						imgHelp.getWidth() + offX, imgHelp.getHeight() + helpY)));
		menuItemActions.add(new MenuItemAction(GameAction.MORE_GAME, new Rect(
				offX, moreGameY, imgMoreGame.getWidth() + offX, imgMoreGame
						.getHeight()
						+ moreGameY)));

		menuItemActions.add(new MenuItemAction(GameAction.QUIT, new Rect(0,
				quitY, imgQuit.getWidth(), imgQuit.getHeight() + quitY)));
		// mainApp.switchBackMusic();
		
		
	}

	@Override
	public void draw() {
		// lock canvas
		Canvas canvas = holder.lockCanvas();

		if (null != canvas) {
			canvas.drawBitmap(imgMenuBack, 0, 0, paint);
			canvas.drawBitmap(imgStart, offX, offY, paint);
			// canvas.drawBitmap(imgOpt, offX, optY, paint);
			// canvas.drawBitmap(imgRes, offX, resY, paint);
			canvas.drawBitmap(imgHelp, offX, helpY, paint);
			canvas.drawBitmap(imgMoreGame, offX, moreGameY, paint);
			canvas.drawBitmap(imgQuit, 0, quitY, paint);

			if (drawState == stateHelp) {
				canvas.drawBitmap(imgGameHelpText, gameHelpX, gameHelpY, paint);
			}
			
			// unlock canvas
			holder.unlockCanvasAndPost(canvas);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		if (drawState == stateHelp) {
			drawState = stateNormal;
		} else {
			switch (Util.checkItemSelect(menuItemActions, event)) {
			case GameAction.START:
				mainApp.playBtnClick();
				mainApp.startNewGame();
				break;
			// case GameAction.OPTION:
			// mainApp.showOption();
			// break;
			// case GameAction.TOP_SCORE:
			// mainApp.showTopScore();
			// break;
			case GameAction.HELP:
				mainApp.playBtnClick();
				drawState = stateHelp;
				break;
			case GameAction.MORE_GAME:
				mainApp.playBtnClick();
				mainApp.showMoreGame();
				break;
			case GameAction.QUIT:
				mainApp.playBtnClick();
				mainApp.exitGame();
				break;
			default:
				break;
			}
		}
		return super.onTouchEvent(event);
	}
}
